﻿using System;
using System.Reflection;
using WPNextTools.NBase;
using WPNextTools.NCommon;

namespace WPNextTools.NControls
{
    internal class ExplodeVirtualizationStrategy : VirtualizationStrategy
    {
        protected override bool CaculateItemParams(IDataSourceItem dataSourceItem)
        {
            int lowestIndex = GetLowestIndex();
            double imgRatio = 1.0;
            if (dataSourceItem.Value is IImageModel)
            {
                imgRatio = ((IImageModel)dataSourceItem.Value).ImgRatio;
            }
            else
            {
                PropertyInfo property = dataSourceItem.Value.GetType().GetProperty("ImgRatio");
                if (property != null && property.CanRead)
                {
                    imgRatio = (double)property.GetValue(dataSourceItem.Value);
                }
                else
                {
                    throw new InvalidOperationException("It's no implements IImageModel!");
                }
            }

            double width = GetColumnWidth();
            double height = width * imgRatio;
            double x = (columnMarginCache + width) * lowestIndex + base.leftRightMarginCache;
            double y = columnY[lowestIndex] + columnMarginCache;
            if (columnY[lowestIndex] == 0.0)
            {
                y = 0.0;
            }
            dataSourceItem.Width = width;
            dataSourceItem.Height = height;
            dataSourceItem.X = x;
            dataSourceItem.Y = y;
            dataSourceItem.ColumnIndex = lowestIndex;
            columnY[lowestIndex] = y + height;

            double virtualY = y - this.owner.manipulationContainer.VerticalOffset;
            if (virtualY >= -NEnviroment.ScrreenHeight && virtualY <= NEnviroment.ScrreenHeight * 2)
            {
                return true;
            }
            return false;
        }
    }
}
